<?php
use
//Nette\Forms\Form,
     Nette\Mail\Message,
     Addons\Tabella,
     Nette\Application\UI\Form,
     Nette\Utils\Html,
     Nette\Utils\Strings;
/**
 * PovodenPresenter presenter.
 *
 * @author     Jakub Šefčik
 * @package    Povoden.com ø 2012
 */
class PovodenPresenter extends BasePresenter
{

	public function renderDefault()
	{

	}
        public function renderFotogalerie()
	{
            $this->template->alba=$this->picasaAlbum();
	}
        public function renderFoto($album)
	{
            $this->template->alba=$this->picasaFotky($album);
	}
        public function picasaFotky($album)
        {
                $fotky = array();
                $i=0;
                $doc = new DomDocument();
                $doc->load("https://picasaweb.google.com/data/feed/api/user/skautpovoden/album/".$album."?thumbsize=221c&imgmax=950");//&tag=vanoce&authkey=Gv1sRgCOP5gf-zoLvRSw");
                $zaklad = $doc->getElementsByTagName("feed");
                $ns_media = $doc->getElementsByTagName('feed' )->item(0)->getAttribute('xmlns:media' );
                $ns_gphoto = $doc->getElementsByTagName('feed' )->item(0)->getAttribute('xmlns:gphoto' );
                foreach($zaklad as $b)
                {
                    $prvky = $b->getElementsByTagName("entry");
                    foreach($prvky as $c)
                    {
                        $fotky[$i]["nazevalba"]=$b->getElementsByTagName("title")->item(0)->textContent;
                        $fotky[$i]["nazevalbapopis"]=$b->getElementsByTagName("subtitle")->item(0)->textContent;

                        //$fotky[$i]["nazev"]=chunk_split($c->getElementsByTagName("title")->item(0)->textContent,"26"," <br>");
                        $fotky[$i]["nazev"]=$c->getElementsByTagName("title")->item(0)->textContent;
                        $fotky[$i]["popisek"]=$c->getElementsByTagName("summary")->item(0)->textContent;
                        $fotky[$i]["datum"]=substr($c->getElementsByTagName("published")->item(0)->textContent, 0,10);
                        $fotky[$i]["typ"]=$c->getElementsByTagName("rights")->item(0)->textContent;

                        $fotky[$i]["albumid"]=$c->getElementsByTagNameNS($ns_gphoto,"id")->item(0)->textContent;
                        $fotky[$i]["albumname"]=$c->getElementsByTagNameNS($ns_gphoto,"name")->item(0)->textContent;
                        $fotky[$i]["pocet"]=$c->getElementsByTagNameNS($ns_gphoto,"numphotos")->item(0)->textContent;

                        $fotky[$i]["nahled"]=$c->getElementsByTagNameNS($ns_media,"thumbnail")->item(0)->getAttribute( 'url' );
                        $fotky[$i]["vetsi"]=$c->getElementsByTagNameNS($ns_media,"content")->item(0)->getAttribute( 'url' );
                        //$fotky[$i]["stitky"]=$c->getElementsByTagNameNS($ns_media,"keywords")->item(0)->textContent;


                        $i++;
                    }   
                }
                return $fotky;
        }
        public function picasaAlbum()
        {
                $fotky = array();
                $i=0;
                $doc = new DomDocument();
                $doc->load("https://picasaweb.google.com/data/feed/api/user/skautpovoden?thumbsize=221c"); //&tag=vanoce&authkey=Gv1sRgCOP5gf-zoLvRSw
                //Child node must be scalar or Html object, DibiDateTime given.
                $zaklad = $doc->getElementsByTagName("feed");
                $ns_media = $doc->getElementsByTagName('feed' )->item(0)->getAttribute('xmlns:media' );
                $ns_gphoto = $doc->getElementsByTagName('feed' )->item(0)->getAttribute('xmlns:gphoto' );

                foreach($zaklad as $b)
                {
                    $prvky = $b->getElementsByTagName("entry");
                    foreach($prvky as $c)
                    {
                        //$fotky[$i]["nazev"]=chunk_split($c->getElementsByTagName("title")->item(0)->textContent,"26"," <br>");
                        $fotky[$i]["nazev"]=$c->getElementsByTagName("title")->item(0)->textContent;
                        $fotky[$i]["popisek"]=$c->getElementsByTagName("summary")->item(0)->textContent;
                        $datum=substr($c->getElementsByTagName("published")->item(0)->textContent, 0,10);
                        $datum2= $pole=explode("-",$datum);
                        $datum3= $pole[2].".".$pole[1].".".$pole[0];
                        $fotky[$i]["datum"]= $datum3;
                        $fotky[$i]["typ"]=$c->getElementsByTagName("rights")->item(0)->textContent;

                        $fotky[$i]["albumid"]=$c->getElementsByTagNameNS($ns_gphoto,"id")->item(0)->textContent;
                        $fotky[$i]["albumname"]=$c->getElementsByTagNameNS($ns_gphoto,"name")->item(0)->textContent;
                        $fotky[$i]["pocet"]=$c->getElementsByTagNameNS($ns_gphoto,"numphotos")->item(0)->textContent;

                        $fotky[$i]["nahled"]=$c->getElementsByTagNameNS($ns_media,"thumbnail")->item(0)->getAttribute( 'url' );
                        $fotky[$i]["vetsi"]=$c->getElementsByTagNameNS($ns_media,"content")->item(0)->getAttribute( 'url' );
                        $fotky[$i]["stitky"]=$c->getElementsByTagNameNS($ns_media,"keywords")->item(0)->textContent;

                        $i++;
                    }
                }



                return $fotky;
        }
     	public function renderForum() 
	{
            $this->redirect('Forum:default');
	}
        public function renderStredisko()
	{
            $this->template->alba=$this->picasaAlbum(); //TODO: optimalizovat
            
            $this->template->kronika = $this->getModels()->Stredisko->getKronika();
            $this->template->forum = $this->getModels()->Stredisko->getStrediskoForum();
            $this->template->aktuality = $this->getModels()->Stredisko->getAktuality();
		
        }
        public function renderKronika()
	{

            $this->template->kronika = $this->getModels()->Stredisko->getKronika();

        }
        public function renderOdkazovnik()
	{

            //$this->template->odkazy = $this->getModels()->Stredisko->getOdkazovnik();

        }
        protected function createComponentOdkazTabella()
        {
		$grid = new Tabella( $this->getModels()->Stredisko->getOdkazy(), array(
                                    'order' => "id","rowRenderer" => function( $row ) {
                                            return Html::el( "tr class='Btext'" );
                                            }
                                        )
                                    );
                $grid->addColumn( "Odkaz", "text", array( "width" => 300 , 'renderer' => function( $row )
			{
				$a = Html::el( 'a' )->href( "http://".$row->url)->setText($row->text);
				return Html::el( "td" )->add($a);
			}));
		$grid->addColumn( 'Popis', 'popis', array('width' => 500));
		$grid->addColumn( 'Sekce', 'nazev', array('width' => 100,'filter' => true));
                return $grid;
	}
        public function renderAktuality()
	{

            $this->template->aktuality = $this->getModels()->Stredisko->getAktuality();

        }
        public function renderKalendar($oddil)
	{
            $pozadi='&amp;bgcolor=%23C8C866';
            $Isara='&amp;src=povoden.com_beqd6fogp822peelufssgf4sr8%40group.calendar.google.com&amp;color=%23A32929';//'%23711616';
            $fialky='&amp;src=povoden.com_4hilcoddkd6s5hip6rg1m84sus%40group.calendar.google.com&amp;color=%2323164E';
            $Stredisko='&amp;src=povoden.com_65vih5t2hpt751utqup7ivnfjo%40group.calendar.google.com&amp;color=%23333333';
            $svvl='&amp;src=povoden.com_adlljjg79o97a14j8voo6bbpj8%40group.calendar.google.com&amp;color=%23AB8B00';
            $Petka='&amp;src=povoden.com_ptujvju3cpnev7vai2spcogrl8%40group.calendar.google.com&amp;color=%231B887A';
            $Vudci='&amp;src=povoden.com_gs6h8rc3ituf2jg91qdbiejt7k%40group.calendar.google.com&amp;color=%23FFFFFF';//'%232F6309';
            $zbytek='&amp;ctz=Europe%2FPrague';

            if ($oddil == 10) $odd=$Stredisko;
            elseif ($oddil == 1) $odd=$Isara;
            elseif ($oddil == 2) $odd=$fialky;
            elseif ($oddil == 3) $odd=$svvl;
            elseif ($oddil == 4) $odd=$svvl;
            elseif ($oddil == 34) $odd=$svvl;
            elseif ($oddil == 5) $odd=$Petka;
            else $odd=$fialky.$Isara.$Stredisko.$Petka.$svvl.$Vudci;

            $oddi=$pozadi.$odd.$zbytek;
            $this->template->oddil = $oddi;

        }
        public function createComponentDotazovnikForm()
	{
             $cislo = rand(1, 5);
             $cislo2 = rand(1, 5);
             $cislokontrola= ($cislo + $cislo2);
            //Nette\Extras\Captcha::register();
            //class_exists('Captcha');
            $form = new Nette\Application\UI\Form;
            $form->addText('email', 'Váš email:')
                    ->addRule(Form::EMAIL, 'Váš email: zadejte adresu ve správem tvaru!');
            $form->addText('predmet', 'Předmět vzkazu:')
                    ->addRule(Form::MIN_LENGTH, 'Předmět vzkazu: alespoň 3 znaky!',3)
                    ->addRule(Form::MAX_LENGTH, 'Předmět vzkazu: zkraťte na 30 znaků!',30);
            $form->addTextArea('vzkaz', 'Text vzkazu:')
                    ->addRule(Form::MIN_LENGTH, 'Text vzkazu: alespoň 3 znaky!',3);
            $stroj = array('ano' => 'ANO','ne' => 'NE');
            $form->addSelect('kontrola', 'Jsem stroj?',$stroj)
                    ->addRule(Form::EQUAL,'Jsi opravdu robot?',ne);
            /*$form->addCaptcha('captcha', 'Antispam')
                    ->setTextColor(Image::rgb(255, 0, 0))
                    ->addRule('Captcha::validateValid', 'Opište správně písmena z obrázku.');*/
            $form->addSubmit('send', 'Odeslat');
            $form->onSuccess[] = callback($this, 'dotazovnikFormSubmitted');
            return $form;
         }
        public function dotazovnikFormSubmitted($form)
        {
            $data = $form->getValues();

            $mail = new Message;
            $mail->setFrom($data->email)
                ->addTo('vedeni@povoden.com')
                //->addTo('jirka@example.com')
                ->setSubject($data->predmet)
                ->setBody($data->vzkaz)
                ->send();

            $this->flashMessage('Dotaz odeslán ...');

             //$form->addError('eee');
             $this->redirect('Povoden:dotazovnik');

         }
        public function createComponentOdkazovnikForm()
	{
            $form = new Form;
            $form->addText('url', 'Odkaz:')
                    ->addRule(Form::MIN_LENGTH, 'Text musí obsahovat alespoň 3 znaky!',3)
                    ->setRequired('Zadávejte BEZ "http://"');
            $form->addText('text', 'Nazev odkazu')
                    ->addRule(Form::MIN_LENGTH, 'Text musí obsahovat alespoň 3 znaky!',3);
            $form->addTextArea('popis', 'Popis:');
            $sekce = $this->getModels()->stredisko->getOdkazySekce()->fetchPairs('id_sekce', 'nazev');
            $form->addSelect('sekce', 'Sekce:', $sekce)
                    ->setPrompt('Zvolte sekci');
            $form->addSubmit('send', 'Odeslat');
            $form->onSuccess[] = callback($this, 'odkazovnikFormSubmitted');
            return $form;
         }
        public function odkazovnikFormSubmitted($form)
        {
            if($this->user->isLoggedIn())
            {
                $data = $form->getValues();
                $this->getModels()->stredisko->addOdkaz($data->url,$data->text,$data->popis,$data->sekce);
                $this->getModels()->Logovani->addLog($this->user->identity->getId(),"odkazovnik","přidán odkaz:".$data->text);//$kdo,$kde,$co,$coid= null
                $this->flashMessage('<p>Odkaz '.$data->text.' = '.$data->url.'</p> <p>pridán ...</p>');
                $this->redirect('Povoden:odkazovnik');

            }
            else
            {
                $form->addError('Nejste přihlášeni');
                $this->redirect('Admin:');

            }

         }

        /*public function renderDefault()
	{
		$this->template->form = $this->getComponent('form');
	}*/
	/*protected function createComponent($name)
	{
		switch ($name) {
		case 'form':
			class_exists('Captcha');
			$form = new AppForm($this, $name);
			$form->addCaptcha('captcha', 'Antispam')
				->setTextColor(Image::rgb(255, 0, 0))
				->addRule('Captcha::validateValid', 'Opište správně písmena z obrázku.');
			$form->addSubmit('send', 'Odeslat')
				->onClick[] = array($this, 'formSubmitted');
			return;

		default:
			parent::createComponent($name);
			return;
		}
	}*/
        /*public function renderOddil0($oddil)
	{
             
             $this->template->oddilAktuality = $this->getModels()->Stredisko->getOddilyAktuality($oddil);
             $this->template->oddil=$oddil;
             if ($oddil==2 ) $this->template->cleni = $this->getModels()->Stredisko->getOddily($oddil,'fialky');
             elseif ($oddil==4 )$this->template->cleni = $this->getModels()->Stredisko->getOddily($oddil,'svetlusky');
             elseif ($oddil==3 )$this->template->cleni = $this->getModels()->Stredisko->getOddily($oddil,'vlcata');
             elseif ($oddil==5 )$this->template->cleni = $this->getModels()->Stredisko->getOddily($oddil,'petka');
             else
             {
                $this->flashMessage('Neznámý oddíl!');
                $this->redirect('Povoden:stredisko');
             }


        }*/
        public function renderOddil($oddil)
	{
             if (($oddil != 2) AND ($oddil != 34) AND ($oddil != 5) )
             {
                $this->flashMessage('Neznámý oddíl!');
                $this->redirect('Povoden:stredisko');
             }
             // \Nette\Diagnostics\Debugger::dump($oddil);
             elseif($oddil == 34)
             {
                 $where="(cle.oddil = '34' or cle.nick = 'Macawi') AND cle.aktivni = '1'";
             }
             /*elseif($oddil == 4)
             {
                 $where="(cle.oddil = '4' or cle.nick = 'Macawi') AND cle.nick != 'Milka' AND cle.aktivni = '1'";
             }
             elseif($oddil == 3)
             {
                 $where="(cle.oddil = '3' or cle.nick = 'Milka')  AND cle.aktivni = '1'";
             }*/
             else
             {
                 $where="cle.aktivni = '1' AND cle.oddil = '".$oddil."'";
             }
             
             $this->template->oddily = $this->getModels()->Oddily->getOddilPreloz($oddil);
             $this->template->id = $oddil;           
             $this->template->cleni = $this->getModels()->Oddily->getCleni($where);
             $this->template->otazky = $this->getModels()->Oddily->getOtazky($where);
             $this->template->aktuality = $this->getModels()->Oddily->getAktuality($oddil);
             $this->template->frcky = $this->getModels()->Oddily->getFrcky($where);
             //$this->template->druziny = $this->getModels()->Oddily->getDruziny($where);

        }

}
